/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service.impl;

import dao.SanPhamDAO;
import java.util.List;
import pojo.SanPham;
import service.SanPhamService;

/**
 *
 * @author MongKieu
 */
public class SanPhamServiceImpl implements SanPhamService {

    private SanPhamDAO sanPhamDAO;

    public SanPhamDAO getSanPhamDAO() {
        return sanPhamDAO;
    }

    public void setSanPhamDAO(SanPhamDAO sanPhamDAO) {
        this.sanPhamDAO = sanPhamDAO;
    }

    @Override
    public int soLuongSanPhamTheoTheLoai(int maTheLoai) {
        return sanPhamDAO.soLuongSanPhamTheoTheLoai(maTheLoai);
    }

    @Override
    public List<SanPham> listSanPhamTheoTheLoai(int maTheLoai) {
        return sanPhamDAO.listSanPhamTheoTheLoai(maTheLoai);
    }

    @Override
    public int tinhSoLuongTrang(int maTheLoai) {
        int soTrang = 0;
        int soLuongSanPhamTrenTrang = 5;
        int soLuong = sanPhamDAO.soLuongSanPhamTheoTheLoai(maTheLoai);
        soTrang = soLuong / soLuongSanPhamTrenTrang;
        if (soLuong % soLuongSanPhamTrenTrang != 0) {
            soTrang++;
        }
        return soTrang;
    }

    @Override
    public List<SanPham> listSanPhamTheoTheLoaiCoPhanTrang(int maTheLoai, int trang) {
        int soSanPhamTrenTrang = 5;
        int batDau = (trang - 1) * soSanPhamTrenTrang;
        return sanPhamDAO.listSanPhamTheoTheLoaiCoPhanTrang(maTheLoai, batDau, soSanPhamTrenTrang);
    }

    @Override
    public boolean kiemTraSanPhamTonTai(int maSanPham) {
        return sanPhamDAO.kiemtraSanPhamTonTai(maSanPham);
    }

    @Override
    public SanPham laySanPhamByMaSanpPham(int maSanPham) {
        return sanPhamDAO.laySanPhamByMaSanPham(maSanPham);
    }

    @Override
    public boolean kiemtraSanPhamTonTaiByISBN(String ISBN) {
        return sanPhamDAO.kiemtraSanPhamTonTaiByISBN(ISBN);
    }

    @Override
    public SanPham laySanPhamByISBN(String ISBN) {
        return sanPhamDAO.laySanPhamByISBN(ISBN);
    }

    @Override
    public List<SanPham> listSanPhamMoiNhapVe(int soLuongSanPham, String loaiTimKiem) {
        String myQuery=creatQueryChoSanPhamMoiVaSapPhatHanh(loaiTimKiem);
        return sanPhamDAO.listSanPhamMoiNhapVe(soLuongSanPham, myQuery);
    }

    @Override
    public int soLuongSanPhamMoiNhapVe(int soLuongMax, String loaiTimKiem) {
        String myQuery=creatQueryChoSanPhamMoiVaSapPhatHanh(loaiTimKiem);
        return sanPhamDAO.listSanPhamMoiNhapVe(soLuongMax, myQuery).size();
    }

    @Override
    public List<SanPham> listSanPhamMoiNhapVeCoPhanTrang(int trang, int soSanPhamTrenTrang, String loaiTimKiem) {
        //int soSanPhamTrenTrang = 5;
        int batDau = (trang - 1) * soSanPhamTrenTrang;
        String myQuery=creatQueryChoSanPhamMoiVaSapPhatHanh(loaiTimKiem);
        return sanPhamDAO.listSanPhamMoiNhapVeCoPhanTrang(batDau, soSanPhamTrenTrang, myQuery);
    }

    @Override
    public int tinhSoLuongTrangChoSanPhamMoiNhapVe(int soLuongSanPhamTrenTrang, int soLuongSanPhamToiDa, String loaiTimKiem) {
        int soTrang = 0;
        String myQuery=creatQueryChoSanPhamMoiVaSapPhatHanh(loaiTimKiem);
        int soLuong = sanPhamDAO.listSanPhamMoiNhapVe(soLuongSanPhamToiDa,myQuery ).size();
        soTrang = soLuong / soLuongSanPhamTrenTrang;
        if (soLuong % soLuongSanPhamTrenTrang != 0) {
            soTrang++;
        }
        return soTrang;
    }

    @Override
    public int soLuongAllSanPhamTrongNhaSach() {
        return sanPhamDAO.soLuongAllSanPhamTrongNhaSach();
    }

    @Override
    public List<SanPham> listAllSanPhamTrongNhaSach(int viTriBatDau, int soLuongSanPham) {
        return sanPhamDAO.listAllSanPhamTrongNhaSachCoPhanTrang(viTriBatDau, soLuongSanPham);
    }

    @Override
    public List<SanPham> listSanPhamTheoTenVaNhomSanPhamCoPhanTrang(String tenSanPham, int NhomSanPhamID, int trang, int soLuongSanPhamTrenTrang) {
        int batDau = (trang - 1) * soLuongSanPhamTrenTrang;
        String myQuery = createMyQueryChoTimKiemTheoTenVaNhomSanPham(tenSanPham, NhomSanPhamID);
        return sanPhamDAO.listSanPhamTheoTenVaNhomSanPhamCoPhanTrang(tenSanPham, NhomSanPhamID, batDau, soLuongSanPhamTrenTrang, myQuery);
    }

    @Override
    public List<SanPham> listSanPhamTheoTenVaNhomSanPham(String tenSanPham, int NhomSanPhamID) {
        String myQuery = createMyQueryChoTimKiemTheoTenVaNhomSanPham(tenSanPham, NhomSanPhamID);
        return sanPhamDAO.listSanPhamTheoTenVaNhomSanPham(tenSanPham, NhomSanPhamID, myQuery);
    }

    @Override
    public int tinhSoLuongTrangChoSanPhamTimKiemTheoTenVaNhomSanPham(String tenSanPham, int NhomSanPhamID, int soLuongSanPhamTrenTrang) {
        int soTrang = 0;
        String myQuery = createMyQueryChoTimKiemTheoTenVaNhomSanPham(tenSanPham, NhomSanPhamID);
        int soLuong = sanPhamDAO.listSanPhamTheoTenVaNhomSanPham(tenSanPham, NhomSanPhamID, myQuery).size();
        soTrang = soLuong / soLuongSanPhamTrenTrang;
        if (soLuong % soLuongSanPhamTrenTrang != 0) {
            soTrang++;
        }
        return soTrang;
    }

    @Override
    public int soLuongSanPhamimKiemTheoTenVaNhomSanPham(String tenSanPham, int NhomSanPhamID) {
        String myQuery = createMyQueryChoTimKiemTheoTenVaNhomSanPham(tenSanPham, NhomSanPhamID);
        return sanPhamDAO.listSanPhamTheoTenVaNhomSanPham(tenSanPham, NhomSanPhamID, myQuery).size();
    }

    @Override
    public String createMyQueryChoTimKiemTheoTenVaNhomSanPham(String tenSanPham, int NhomSanPhamID) {
        String MyQuery = "";
        if (NhomSanPhamID > 0) {
            MyQuery = "from SanPham where theLoai.nhomSanPham.id=" + NhomSanPhamID + "and TenSanPham like '%" + tenSanPham + "%'";
        } else if (NhomSanPhamID == -3)//Ten / Tua
        {
            MyQuery = "from SanPham where  TenSanPham like '%" + tenSanPham + "%'";
        } else if (NhomSanPhamID == -1) {//Isbn
            MyQuery = "from SanPham Where ISBN=" + tenSanPham;
        } else if (NhomSanPhamID == 0) {//Tac gia
            MyQuery = "from SanPham where  TenTacGia like '%" + tenSanPham + "%'";
        }
        return MyQuery;
    }

    @Override
    public String creatQueryChoSanPhamMoiVaSapPhatHanh(String loaiTimKiem) {
        String myQuery = "";
        if (loaiTimKiem.equals("Moi")) {
            myQuery = "from SanPham order by NgayNhap- current_timestamp()  desc ";
        } else if (loaiTimKiem.equals("SapPhatHanh")) {
            myQuery = "from SanPham group by ISBN having (NgayXuatBan- current_timestamp())>0 order by NgayXuatBan- current_timestamp()  desc";
        }
        return myQuery;

    }
}
